﻿using System;
using BizUnit;
using BTS.Testing.TraceMonitor.BizUnitSteps.Helpers;

namespace BTS.Testing.TraceMonitor.BizUnitSteps
{
    /// <summary>
    /// Polls the list of all events to look for one which matches criteria
    /// </summary>
    public class PollForEvent : BizUnit.BizUnitOM.ITestStepOM 
    {
        public string RegEx { get; set; }
        public bool IncludeBodyCompare { get; set; }
        public int TimeoutSeconds { get; set; }
        public bool DeleteAfterFound { get; set; }
        public string QueuePath { get; set; }

        /// <summary>
        /// Execute step
        /// </summary>
        /// <param name="testConfig"></param>
        /// <param name="context"></param>
        public void Execute(System.Xml.XmlNode testConfig, Context context)
        {
            RegEx = context.ReadConfigAsString(testConfig, "RegEx");
            IncludeBodyCompare = context.ReadConfigAsBool(testConfig, "IncludeBodyCompare");
            TimeoutSeconds = context.ReadConfigAsInt32(testConfig, "TimeoutSeconds");
            DeleteAfterFound = context.ReadConfigAsBool(testConfig, "DeleteAfterFound");
            QueuePath = context.ReadConfigAsString(testConfig, "QueuePath");

            Execute(context);
        }

        public void Execute(Context context)
        {
            Execute();
        }

        public void Execute()
        {
            bool found = EventTraceManager.PeekForAnyEvent(RegEx, IncludeBodyCompare, TimeoutSeconds, DeleteAfterFound, QueuePath);
            if (!found)
                throw new ApplicationException("The event has not been found");
        }

        public void Validate(Context context)
        {
            
        }
    }
}
